import { createStore, applyMiddleware } from 'redux'
import createSagaMiddleware from 'redux-saga'

import rootSaga from '../sagas'

import rootReducer from '../reducers'

const configureStore = preloadedState => {
    const sagaMiddleware = createSagaMiddleware()

    const store = createStore(
        rootReducer,
        preloadedState,
        applyMiddleware(sagaMiddleware)
    )

    //开发环境使用
    if (process.env.NODE_ENV !== 'production' && module.hot) {
        // Enable Webpack hot module replacement for reducers
        module.hot.accept('../reducers', () => {
            store.replaceReducer(rootReducer.default)
        })
    }

    store.runSaga = sagaMiddleware.run

    return store
}

const store = configureStore()
store.runSaga(rootSaga)

export default store



// WEBPACK FOOTER //
// ./src/store/index.js